/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.controller;

import java.util.List;
import edu.syr.iis.web.model.DialogueActLabel;
import edu.syr.iis.web.model.DialogueActLabelManager;
import edu.syr.iis.web.model.DialogueActScheme;
import edu.syr.iis.web.model.DialogueActSchemeManager;
import edu.syr.iis.web.util.Key;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 * @author kinoue
 *
 */
public class DialogueActLabelListController extends AbstractController
{
    DialogueActLabelManager dialogueActLabelManager;
    DialogueActSchemeManager dialogueActSchemeManager;
    
    private String successView;
        
    @Override
	public ModelAndView handleRequestInternal(HttpServletRequest request,
	    HttpServletResponse arg1) throws Exception
    {        	
	ModelAndView modelAndView = new ModelAndView(getSuccessView());
	/*
	 *  If DEL_DALABEL_ID is specified, delete the item first.
	 */
	if (request.getParameter(Key.DEL_DA_LABEL_ID) != null
                && request.getParameter(Key.DEL_DA_LABEL_ID).trim().length() > 0) 
	{
            int labelId = Integer.parseInt(request.getParameter(Key.DEL_DA_LABEL_ID));
            dialogueActLabelManager.deleteDialogueActLabel(labelId);
	}
	
	List<DialogueActLabel> dialogueActLabels = null;
	/*
	 *  If DASCHEME_ID is specified, return only the DA Labels with in the DA Schemes.
	 */
	if (request.getParameter(Key.DA_SCHEME_ID) != null
                && request.getParameter(Key.DA_SCHEME_ID).trim().length() > 0) 
	{
            int schemeId = Integer.parseInt(request.getParameter(Key.DA_SCHEME_ID));
            DialogueActScheme scheme = dialogueActSchemeManager.getDialogueActScheme(schemeId);
            dialogueActLabels = dialogueActLabelManager.getDialogueActLabelsByScheme(scheme, false);    
	} else {
		
		int schemeId = 145; //This just happens to my coding scheme;
		
		DialogueActScheme scheme = dialogueActSchemeManager.getDialogueActScheme(schemeId);
        dialogueActLabels = dialogueActLabelManager.getDialogueActLabelsByScheme(scheme, false);    
	    //dialogueActLabels = dialogueActLabelManager.getDialogueActLabels(false);    
	}	
	
	
	List<DialogueActScheme> dialogueActSchemes = dialogueActSchemeManager.getDialogueActSchemes();
	
	modelAndView.addObject(Key.DA_LABELS, dialogueActLabels);
	modelAndView.addObject(Key.DA_SCHEMES, dialogueActSchemes);
        return modelAndView; 
    }

    public void setDialogueActLabelManager(DialogueActLabelManager dialogueActLabelManager)
    {
        this.dialogueActLabelManager = dialogueActLabelManager;
    }

    public String getSuccessView()
    {
        return successView;
    }

    public void setSuccessView(String successView)
    {
        this.successView = successView;
    }

    public void setDialogueActSchemeManager(
    	DialogueActSchemeManager dialogueActSchemeManager)
    {
        this.dialogueActSchemeManager = dialogueActSchemeManager;
    }
    
    
}
